/*
 * Copyright (c) 2024-2025 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef ENDPOINT_H
#define ENDPOINT_H

#include <stdlib/stdint.h>

#include <tools/list.h>

#include <core/object.h>

typedef enum { EP_IDLE, EP_SEND, EP_RECV, EP_DIED } ep_state_t;

typedef struct endpoint {
    object_t   obj;
    list_t     waitlist;
    ep_state_t state;
    uint8_t    free;
    spinlock_t lock;
} DEFAULT_ALINMENT endpoint_t;

static inline void endpoint_get(endpoint_t *ep)
{
    object_get(&ep->obj); /* PRQA S 3200 */
}

static inline void endpoint_put(endpoint_t *ep)
{
    object_put(&ep->obj, offset_of(endpoint_t, obj), sizeof(endpoint_t)); /* PRQA S 3200, 3432 */
}

void    endpoint_init(endpoint_t *ep);
void    endpoint_send(endpoint_t *ep, uint64_t tag, uint64_t m0, uint64_t m1, void *_sender);
void    endpoint_receive(endpoint_t *ep, uint64_t tag, void *receiver);
void    endpoint_call(endpoint_t *ep, uint64_t tag, uint64_t m0, uint64_t m1, void *caller);
int32_t endpoint_reply(endpoint_t *ep, uint64_t tag, uint64_t m0, uint64_t m1, void *replier);
void    endpoint_cancel(endpoint_t *ep);
void    endpoint_clean_callers(void *tsk);

endpoint_t *get_faulthandler_endpoint(void);
void        faulthandler_endpoint_init(void);

#endif
